<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\25 0025
 * Time: 14:47
 */

namespace app\Lib\Model\SysConfig\SetConfig;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\AdminConfig;
use app\Lib\Model\SysConfig\SysConfig;

class SetConfigValue implements  \app\Lib\InterfaceIn\SysConfig\SetConfig\SetConfigValue
{
    /** @var UserHandle $opUser */
    private $opUser;
    private $post;
    public function __construct(UserHandle $opUser)
    {
        $this->opUser = $opUser;
    }

    /**
     * 设置提交参数
     * @param $post
     * @return \app\Lib\InterfaceIn\SysConfig\SetConfig\SetConfigValue
     */
    public function setPost($post)
    {
        $this->post = $post;
        return $this;
    }

    /**
     * 执行代码
     * @return bool
     * @throws BlException
     */
    public function run()
    {
        if(!is_array($this->post)){
            throw new BlException('参数设置错误');
        }
        $model = (new AdminConfig());
        foreach ($this->post as $k=>$v){
            $result = $model->where($model->_variable,$k)->update($v);
            if($result === false){
                throw new BlException('配置名称为'.$k.'，修改失败');
            }
            /*删除缓存*/
            SysConfig::delValue($k);
        }
        return true;
    }

}